#include "rasterization.h"
#include "tools.h"
#include "model.h"
#include "tgaimage.h"
#include "geometry.h"


const TGAColor white = TGAColor(255,255,255,255);
const TGAColor red = TGAColor(255,0,0,255);
const TGAColor green = TGAColor(0,255,0,255);
const TGAColor blue = TGAColor(0,0,255,255);

const int width = 1000;
const int height = 1000;

// 计算质心
vec3 barycentric(vec2* pts, vec2 p){
	vec3 u = cross(vec3(pts[2][0] - pts[0][0], pts[1][0] - pts[0][0], pts[0][0] - p[0]),
		vec3(pts[2][1] - pts[0][1], pts[1][1] - pts[0][1], pts[0][1] - p[1]));
	if(std::abs(u[2]) <= 1e-2)return vec3(-1,1,1);
	return vec3(1.f-(u.x+u.y)/u.z, u.y/u.z, u.x/u.z);
}

vec3 barycentric(vec3* pts, vec3 p){
	vec3 u = cross(vec3(pts[2][0] - pts[0][0], pts[1][0] - pts[0][0], pts[0][0] - p[0]),
		vec3(pts[2][1] - pts[0][1], pts[1][1] - pts[0][1], pts[0][1] - p[1]));
	if(std::abs(u[2]) <= 1e-2)return vec3(-1,1,1);
	return vec3(1.f-(u.x+u.y)/u.z, u.y/u.z, u.x/u.z);
}

// 将计算好的投影空间坐标映射到屏幕空间上，0,0点为图像中心位置
vec3 world2screen(vec3 in){
	return vec3((int)((in.x+1.)*width/2.+.5), (int)((in.y+1.)*height/2.+.5), in.z);
}


int main(){
	
	  TGAImage image(width,height,TGAImage::RGB);
	
  	Model* model = new Model("obj/african_head/african_head.obj");
	
  	vec3 light_dir(0,0,-1);

    TGAImage diffuseTex;
    diffuseTex.read_tga_file("obj/african_head/african_head_diffuse.tga"); 
    diffuseTex.flip_vertically();

	  return 0;
}
